package www.reagnetbox.com.util;

import android.util.Log;

import org.jetbrains.annotations.NotNull;

import java.math.BigInteger;

public class PlcModBusUtil {
    //起始符
    private final String START = "3A";
    //帧头
    private final String FRAME_HEADER = "3032";
    //写多个寄存器命令
    private final String WRITE_COMMAND = "3130";
    //写单个寄存器
    private final String WRITE_SIGNLE_COMMAND = "3036";
    //读命令
    private final String READ_COMMAND = "3033";
    //结束符
    private final String END = "0D0A";

    public static final String TAG = "PlcModBusUtil";

    /**
     * 封装modbus，写请求协议
     */
    public String getWriteString(String address, String[] data) {
        StringBuilder stringBuilder = new StringBuilder();
        //起始符
        stringBuilder.append(START);
        //帧头
        stringBuilder.append(FRAME_HEADER);
        //功能码
        if (data.length > 1) {
            stringBuilder.append(WRITE_COMMAND);
        } else {
            stringBuilder.append(WRITE_SIGNLE_COMMAND);
        }


        //地址,多个地址，只传起始地址，16进制转ascii
        stringBuilder.append(hexToAscii(address));

        //多个地址与数据，需要添加长度和位数
        if (data.length>1){
            //长度,例如：写2条数据，长度为2，位数为4，长度补足4位，位数补足2位
            StringBuilder lengthBuilder = new StringBuilder();
            lengthBuilder.append(data.length);
            int length = lengthBuilder.length();
            for (int i = 0; i < 4 - length; i++) {
                lengthBuilder.insert(0, "0");
            }
            //转ascii
            stringBuilder.append(hexToAscii(lengthBuilder.toString()));

            //位数
            StringBuilder digitsBuilder = new StringBuilder();
            digitsBuilder.append(data.length * 2);
            int digitsLength = digitsBuilder.length();
            for (int i = 0; i < 2 - digitsLength; i++) {
                digitsBuilder.insert(0, "0");
            }
            //转ascii
            stringBuilder.append(hexToAscii(digitsBuilder.toString()));
        }

        //写入数据处理
        for (String datum : data) {
            //10进制转16进制
            StringBuilder hexString = new StringBuilder();
            hexString.append(datum);
            int hexLength = hexString.length();
            //转化后的16进制进行补位，目标为4位
            for (int i = 0; i < 4 - hexLength; i++) {
                hexString.insert(0, "0");
            }
            //转ascii，添加数据
            stringBuilder.append(hexToAscii(hexString.toString()));
        }

        //LRC校验结果生成，去掉起始
        stringBuilder.append(getLrc(stringBuilder.substring(START.length())));
        //添加结束符
        stringBuilder.append(END);
        return stringBuilder.toString();
    }

    public String getReadString(String[] address) {
        StringBuilder stringBuilder = new StringBuilder();
        //起始符
        stringBuilder.append(START);
        //帧头
        stringBuilder.append(FRAME_HEADER);
        //读命令
        stringBuilder.append(READ_COMMAND);
        //地址，多个地址，只传起始地址,16进制转ascii
        stringBuilder.append(hexToAscii(address[0]));

        //长度,例如：写读条数据，长度为2,长度补足4位   位数为4，位数补足2位
        StringBuilder lengthBuilder = new StringBuilder();
        lengthBuilder.append(address.length);
        int length = lengthBuilder.length();
        for (int i = 0; i < 4 - length; i++) {
            lengthBuilder.insert(0, "0");
        }
        //转ascii
        stringBuilder.append(hexToAscii(lengthBuilder.toString()));

        //LRC校验结果生成，去掉起始
        stringBuilder.append(getLrc(stringBuilder.substring(START.length())));
        //添加结束符
        stringBuilder.append(END);
        return stringBuilder.toString();
    }

    //返回，是否是读请求的返回数据
    public boolean isReadRequest(String readString) {
        if (readString.length() < 30) {
            System.out.println("收到的数据响应格式不正确");
            return false;
        }
        String startString = START + FRAME_HEADER;
        String command = readString.substring(startString.length(), startString.length()+READ_COMMAND.length() );
        return command.equals(READ_COMMAND);
    }


    public String[] formatReadString(String readString) {
        String startString = START + FRAME_HEADER + READ_COMMAND;
        if (readString.startsWith(startString)) {
            if (readString.length() < 30) {
                System.out.println("收到的数据响应格式不正确");
                return null;
            }
            String contentString = readString.substring(startString.length() + 4, readString.length() - 8);
            //数据为8位一组
            if (contentString.length() % 8 != 0) {
                System.out.println("收到的数据响应格式不正确");
                return null;
            }

            int length = contentString.length() / 8;
            String[] contentArray = new String[length];
            for (int i = 0; i < length; i++) {
                //8位结果
                String data = contentString.substring(i * 8, i * 8 + 8);
                //ascii转16进制
                String hexString = asciiToHex(data);
                //16进制转10进制
                contentArray[i] = num16to10(hexString) + "";
            }
            return contentArray;
        }
        Log.e(TAG, "无效数据");
        return null;
    }

    private String getLrc(String hexdata) {
        StringBuilder rtu = new StringBuilder();
        int dealLength = hexdata.length() / 2;
        for (int i = 0; i < dealLength; i++) {
            String stringAscill = hexdata.substring(2 * i, 2 * i + 2);
            rtu.append(Integer.parseInt(stringAscill) - 30);
        }
        int rtuLength = rtu.length() / 2;
        int sum = 0;
        for (int i = 0; i < rtuLength; i++) {
            sum += Integer.parseInt(rtu.substring(2 * i, 2 * i + 2), 16);
        }

        //求和结果，补位成4位
        StringBuilder sumBuilder = new StringBuilder();
        sumBuilder.append(num10to16(sum).toUpperCase());
        int sumBuilderLength = sumBuilder.length();
        for (int i = 0; i < 4 - sumBuilderLength; i++) {
            sumBuilder.insert(0, "0");
        }
        //补为4位的 只取低位
        String result = sumBuilder.substring(2, sumBuilder.length());
        return hexToAscii(num10to16(Integer.parseInt("100", 16) - Integer.parseInt(result, 16)).toUpperCase());
    }

    private String hexInt(int total) {
        int a = total / 256;
        int b = total % 256;
        if (a > 255) {
            return hexInt(a) + format(b);
        }
        return format(a) + format(b);
    }

    private String format(int hex) {
        String hexa = Integer.toHexString(hex);
        int len = hexa.length();
        if (len < 2) {
            hexa = "0" + hexa;
        }
        return hexa;
    }

    /**
     * 10进制转16进制
     * @param num
     * @return
     */
    private String num10to16(Integer num) {
        return Integer.toHexString(num);
    }

    /**
     * 16进制转10进制
     */
    private int num16to10(String hexString) {
        int result = 0;
        try {
            result = new BigInteger(hexString, 16).intValue();
        } catch (Exception e) {
            Log.e(TAG, "数据解析失败");
        }
        return result;
    }

    /**
     * 16进制转ascii
     * @param hex
     * @return
     */
    public String hexToAscii(String hex) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < hex.length(); i++) {
            char a = hex.charAt(i);
            int aInt = (int) a;
            stringBuilder.append(num10to16(aInt));
        }
        return stringBuilder.toString();
    }

    /**
     * ascii转16进制
     * @param str
     * @return
     */
    private String asciiToHex(@NotNull String str) {
        byte[] hex = fromHex(str);
        StringBuilder result = new StringBuilder();
        for (byte b : hex) {
            result.append((char) b);
        }
        return result.toString();
    }

    /**
     * 字节数组转16进制
     * @param bytes
     * @return
     */
    public String toHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        byte[] arr = bytes;
        int len = bytes.length;

        for (int i = 0; i < len; ++i) {
            byte b = arr[i];
            sb.append(String.format("%02x", b));
        }

        return sb.toString().toUpperCase();
    }

    /**
     * string转16进制字节数组
     * @param string
     * @return
     */
    public byte[] fromHex(String string) {
        byte[] result = new byte[string.length() / 2];

        for (int i = 0; i < result.length; ++i) {
            try {
                result[i] = (byte) Integer.parseInt(string.substring(i * 2, i * 2 + 2), 16);
            } catch (Exception e) {
                Log.e(TAG, "数据解析失败");
            }
        }
        return result;
    }


    /**
     * hex字符串转byte数组
     * @param inHex 待转换的Hex字符串
     * @return 转换后的byte数组结果
     */
    public byte[] hexToByteArray(String inHex) {
        int hexlen = inHex.length();
        byte[] result;
        if (hexlen % 2 == 1) {
            //奇数
            hexlen++;
            result = new byte[(hexlen / 2)];
            inHex = "0" + inHex;
        } else {
            //偶数
            result = new byte[(hexlen / 2)];
        }
        int j = 0;
        for (int i = 0; i < hexlen; i += 2) {
            result[j] = hexToByte(inHex.substring(i, i + 2));
            j++;
        }
        return result;
    }

    /**
     * Hex字符串转byte
     *
     * @param inHex 待转换的Hex字符串
     * @return 转换后的byte
     */
    public byte hexToByte(String inHex) {
        return (byte) Integer.parseInt(inHex, 16);
    }

    /**
     * 字节数组转16进制
     * @param bytes 需要转换的byte数组
     * @return 转换后的Hex字符串
     */
    public static  String bytesToHex(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() < 2) {
                sb.append(0);
            }
            sb.append(hex);
        }
        return sb.toString();
    }
}

